package com.ctrip.demo.leetcode;

import java.util.Arrays;

public class LeetCode324 {

    public static void main(String[] args) {
        new LeetCode324().wiggleSort(new int[]{1,1,2,1,2,2,1});
    }


    public void wiggleSort(int[] nums) {

        int len = nums.length;
        int mid = (len)/ 2;
        int mo = len % 2;

        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        int[] res = new int[len];
        int idx = 0;

        int p1 = 0,p2 = mid;
        if (mo != 0) {
            p2 ++;
        }


        while(p1 <= mid && p2 < len) {
            res[idx ++] = nums[p1++];
            res[idx ++] = nums[p2++];
        }

        if (mo == 0) {
            if (p1 < mid) {
                res[idx++] = nums[p1++];
            }
            if (p2 < len) {
                res[idx++] = nums[p2++];
            }
        } else {
            res[idx ++] = nums[mid];
        }

        System.arraycopy(res, 0, nums, 0, len);
        System.out.println(Arrays.toString(nums));
    }

}
